package cn.cloud.butler.uploadapi.service;


import cn.cloud.butler.uploadapi.configuration.HttpUtil;
import cn.cloud.butler.uploadapi.domain.AlarmDataEntity;
import cn.cloud.butler.uploadapi.domain.ResponseEntityNoData;
import cn.cloud.butler.uploadapi.logger.WaLogger;
import cn.cloud.butler.uploadapi.repository.AlarmDataRepository;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.apache.http.conn.HttpHostConnectException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;

@Service
public class DataTreate {
    String Url1 = "http://10.1.25.99:8107";//正式服
    String Url = "http://10.1.20.114:8107";//测试服平台地址
    String api_path_status = "/api/response/device/status";
    String api_path_alarm = "/api/response/device/warning";

    @Autowired
    private AlarmDataRepository alarmDataRepository;

    public ResponseEntityNoData dealStates(String json) {
        ResponseEntityNoData responseEntity = new ResponseEntityNoData();
        try {
            if (json.contains("state")) {
                json = json.replace("state", "State");
            }
            System.out.println("状态数据转发：" + json);
//            HttpUtil.sendPostDataByJson(Url + api_path_status, json);
            try {
                HttpUtil.sendPostDataByJson(Url1 + api_path_status, json);   //数据转发到平台正式服
            } catch (Exception e) {
                System.out.println("请求异常：" + e.toString());
            }
            if(!json.contains("STATE_ALARM")){
                //解析出设备及时间
                JSONObject jsonObjects = JSONObject.parseObject(json);
                String macSN = jsonObjects.getString("equipment");
                String times = jsonObjects.getJSONArray("data").getJSONObject(0).getString("t");
                String source = jsonObjects.getString("source");
                JSONObject jsonState = new JSONObject();
                jsonState.put("Alarm", null);
                jsonState.put("equipment", macSN);
                jsonState.put("t", times);
                jsonState.put("isForwardCall", 0);
                jsonState.put("source", source);
                String result = JSONObject.toJSONString(jsonState, SerializerFeature.WriteMapNullValue);
                System.out.println("默认alarm转发数据：" + result);
//            HttpUtil.sendPostDataByJson(Url + api_path_alarm, result);
                try {
                    HttpUtil.sendPostDataByJson(Url1 + api_path_alarm, result);
                } catch (Exception e) {
                    System.out.println("请求异常：" + e.toString());
                }
            }
            responseEntity.setMsgCode(1);
            responseEntity.setMsgDesc("上传成功");
        } catch (Exception e) {
            responseEntity.setMsgCode(0);
            responseEntity.setMsgDesc("上传失败： " + e.toString());
            WaLogger.logger.warn(e.toString());
        }
        return responseEntity;
    }

    public ResponseEntityNoData dealAlarms(String json) throws IOException {
        ResponseEntityNoData responseEntity = new ResponseEntityNoData();
        JSONObject jsonObject = JSONObject.parseObject(json);
        try {
            if (json.contains("data")) {
                JSONArray jsonData = jsonObject.getJSONArray("data");
                JSONObject jsonAlarmT = new JSONObject();
                JSONArray jsonAlarm = new JSONArray();
                String jsona = jsonData.getJSONObject(0).getString("v");
                if (!"".equals(jsona)) {
                    for (int i = 0; i < jsonData.size(); i++) {
                        JSONObject jsonObjectAlarm = new JSONObject();
                        String jsons = jsonData.getJSONObject(i).getString("v");
                        JSONObject jsonAlarmv = JSONObject.parseObject(jsons);
                        String number = jsonAlarmv.getString("no");
                        String text = jsonAlarmv.getString("text");
                        jsonObjectAlarm.put("no", number);
                        if (StringUtils.isEmpty(text)) {
                            AlarmDataEntity alarmDataEntity = alarmDataRepository.findByAlarmnum(number);
                            if (alarmDataEntity != null) {
                                jsonObjectAlarm.put("text", alarmDataEntity.getData());
                            } else {
                                jsonObjectAlarm.put("text", "未知报警");
                            }
                        } else {
                            jsonObjectAlarm.put("text", text);
                        }
                        jsonAlarm.add(jsonObjectAlarm);
                    }
                    jsonAlarmT.put("Alarm", jsonAlarm);
                    jsonAlarmT.put("t", jsonData.getJSONObject(0).getString("t"));
                    jsonAlarmT.put("equipment", jsonObject.getString("equipment"));
                    jsonAlarmT.put("source", jsonObject.getString("source"));
                    String alarmJson = jsonAlarmT.toString();
//                    HttpUtil.sendPostDataByJson(Url + api_path_alarm, alarmJson);
                    System.out.println("最终上传的报警数据" + alarmJson);
                    try {
//                        HttpUtil.sendPostDataByJson(Url + api_path_alarm, alarmJson);
                        HttpUtil.sendPostDataByJson(Url1 + api_path_alarm, alarmJson);
                    } catch (Exception e) {
                        System.out.println("请求异常：" + e.toString());
                    }
                    responseEntity.setMsgCode(1);
                    responseEntity.setMsgDesc("上传成功");
                } else {
                    responseEntity.setMsgCode(2);
                    responseEntity.setMsgDesc("报警数据为空的数据");
//                    System.out.println("报警数据为空的数据："+jsona);
                }
            } else {
                if (jsonObject.getString("Alarm") != null) {
                    JSONArray jsonArray = jsonObject.getJSONArray("Alarm");
                    for (int i = 0; i < jsonArray.size(); i++) {
                        JSONObject jsonObjectData = jsonArray.getJSONObject(i);
                        String number = jsonObjectData.getString("no");
                        String text = jsonObjectData.getString("text");
                        if (StringUtils.isEmpty(text)) {
                            AlarmDataEntity alarmDataEntity = alarmDataRepository.findByAlarmnum(number);
                            if (alarmDataEntity != null) {
                                jsonObject.getJSONArray("Alarm").getJSONObject(i).put("text", alarmDataEntity.getData());
                            } else {
                                jsonObject.getJSONArray("Alarm").getJSONObject(i).put("text", "未知报警");
                            }
                        }
                    }
                    json = jsonObject.toJSONString();
                    System.out.println("报警数据" + json);
                }
//
                try {
//                    HttpUtil.sendPostDataByJson(Url + api_path_alarm, json);
                    HttpUtil.sendPostDataByJson(Url1 + api_path_alarm, json);
                    responseEntity.setMsgCode(1);
                    responseEntity.setMsgDesc("上传成功");
                } catch (HttpHostConnectException e) {
                    // 捕获连接超时异常
                    String errorMessage = e.getMessage();
                    // 将JSON字符串返回给前端
                    responseEntity.setMsgCode(0);
                    responseEntity.setMsgDesc(errorMessage);
                }

            }
        } catch (Exception e) {
            responseEntity.setMsgCode(0);
            responseEntity.setMsgDesc("上传失败： " + e.toString());
        }
        return responseEntity;
    }

    public ResponseEntityNoData otherInfo(String json) {
        ResponseEntityNoData responseEntity = new ResponseEntityNoData();
        try {
            JSONObject jsonBSData = JSONObject.parseObject(json);
            if (!StringUtils.isEmpty(jsonBSData.getString("data"))) {
//                HttpUtil.sendPostDataByJson(Url + api_path_status, json);
                try {
                    HttpUtil.sendPostDataByJson(Url1 + api_path_status, json);
                }catch (Exception e){
                    System.out.println(e.toString());
                }

                responseEntity.setMsgCode(1);
                responseEntity.setMsgDesc("上传成功");
            } else {
                responseEntity.setMsgCode(2);
                responseEntity.setMsgDesc("报警数据为空的数据");
            }
        } catch (Exception e) {
            responseEntity.setMsgCode(0);
            responseEntity.setMsgDesc("上传失败： " + e.toString());
        }
        return responseEntity;
    }

    //异步处理数据转发



}
